<template>
  <div>
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3">快递单号：</el-col>
      <el-col :span="20">
        <el-input
          type="input"
          placeholder="请输入内容"
          v-model="currEditInfo.orther"
        />
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3">上传图片：</el-col>
      <el-col :span="20">
        <el-upload
          action="http://47.103.2.155:8801/api/File/upload"
          list-type="picture-card"
          name="file"
          :on-success="(obj,file,list) => onUpload(obj,file,list,idx)"
          :limit="6"
          :file-list="imageList"
          :class="{ hide: hideUploadBtn }"
          :on-remove="onRemoveImageList"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin-bottom: 10px;">
      <el-col :span="3">进度备注：</el-col>
      <el-col :span="20">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="currEditInfo.content"
        />
      </el-col>
    </el-row>
    <el-row :span="24" style="display: flex;justify-content: center;">
      <el-col :span="6" style="text-align: center;">
        <el-button type="primary" @click="onSave">保存</el-button>
        <el-button type="primary" plain @click="onClose">关闭</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { saveTimeLine } from '@/api/yzTimeLine';
import { getCurrentTimeString } from '@/utils/index';

export default {
  name: 'addProgress',
  props: {
    orderInfo: {}
  },
  data() {
    return {
      hideUploadBtn: false,
      currEditInfo: {},
      imageList: []
    }
  },
  methods: {
    onUpload(obj,res,file,idx){
      if (obj.code == 200) {
        this.$message.success({dangerouslyUseHTMLString: true, message: obj.msg})
        this.handleImageList(file)
      } else {
        this.$message.error('操作失败')
      }
    },
    handleImageList(fileList) {
      this.currEditInfo.pictureArray = '';
      this.imageList = fileList.map((item, index) => {
        const url = (item['response'] && item['response']['data'] && item['response']['data']['fileUrl']) || item.url;
        this.currEditInfo.pictureArray += url + (index === fileList.length - 1 ? '' : ',')
        return { name: item.name, url }
      })
      this.hideUploadBtn = this.imageList.length >= 6;
    },
    onRemoveImageList(file, fileList) {
      this.handleImageList(fileList);
    },
    async onSave() {
      const userInfo = sessionStorage.getItem('userInfo');
      const userId = JSON.parse(userInfo)['id'];
      const time = getCurrentTimeString();
      const params = { ...this.currEditInfo, userId, orderId: this.orderInfo.orderId, title: this.orderInfo.orderTakeName, time }
      // console.log('params', params)
      const { data } = await saveTimeLine(params)
      if (data.code === 200) {
        this.$message({
          message: data.msg || '成功',
          type: 'success',
        });
        this.onClose();
      } else {
        this.$message({
          message: data.msg || '失败',
          type: 'error',
          showClose: true,
          duration: 3000,
        });
      }
      this.$emit('refreshList');
    },
    onClose() {
      this.currEditInfo = {};
      this.imageList = [];
      this.hideUploadBtn = false;
      this.$emit('closeToggle');
    }
  }
}
</script>